const router = require('koa-router')()
const Mock=require('mockjs')
const JWT=require("jsonwebtoken")
let userList=[{
  "username":"admin",
  "password":"admin",
  "info":"https://imagepphcloud.thepaper.cn/pph/image/285/216/549.jpg"
},{
  "username":"user",
  "password":"user",
  "info":"https://ww4.sinaimg.cn/mw690/006a9daYgy1hue5g9krqzj30u01sy0yk.jpg"
}]
let {city,searchList}=Mock.mock({
  "city|10":[{
    "title":"@ctitle(2)",
    "children|10-20":[{
      "county":"@county"
    }]
  }],
  "searchList|30":[{
    "title":"@ctitle(2)",
    "img":"@image(100x100,@color)"
  }]
})

router.get('/searchList',(ctx)=>{
  let {value}=ctx.request.query
  console.log(value)
  if (value!=="") {
    let items=searchList.filter((item)=>item.title.includes(value))
    ctx.body={
      code:200,
      items
    }
  }else{
    ctx.body={
      items:searchList
    }
  }
})

router.get('/search',(ctx)=>{
  ctx.body={
    code:200,
    searchList
  }
})
router.get('/city',(ctx)=>{
  ctx.body={
    city,
    msg:"获取成功"
  }
})
router.post('/login', (ctx, next) => {
  let {username,password}=ctx.request.body;
  let items=userList.find((item)=>item.username==username && item.password==password)
  if (items) {
    ctx.body={
      code:200,
      msg:"登录成功",
      token:JWT.sign(items.username,"werftgyhujiko"),
      userInfo:{
        infoImg:items.info,
        infoUser:items.username
      }
    }
  }else{
    ctx.body={
      code:400,
      msg:"登录失败"
    }
  }
})
 
module.exports = router
